//
//  NearbyPharmacy.m
//  Pocket Doctor alpha version
//
//  Created by Sherif, Yin, Taylor, Jacob, Anna, Mike on 11-11-25.
//  Copyright (c) 2011 __Sytjam__. All rights reserved.
//
//Class Description: uses google places in a webview to display all nearby pharmacies.
//
//Known Bugs: none.
//

#import "NearbyPharmacy.h"
#import "MainMenu.h"


#define RADIANS(degrees) ((degrees * M_PI) / 180.0)

@implementation NearbyPharmacy

@synthesize webView, navigationComplete, navigationLoading, lastPageLoading, nextPageLoading, stopLoading, back, lastPage, nextPage, refresh, activityIndicator;
@synthesize mm;

//go back to main menu
- (IBAction)BackToMainMenu:(id)sender{
    
    [self dismissModalViewControllerAnimated:YES];
}

//refresh = go back to google places
//stop would stop the webview from loading the next/current page.
- (IBAction)RefreshOrStop:(id)sender{
        // reconstruct requestObj here, or use a class member
    if ([webView isLoading]){
        [webView stopLoading];
    }
    else{
        [webView  loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.ca/m/places?hl=en#ipd:mode=search&q=pharmacy&source=mus"]]];
    }
}

//go back in stack (built in)
- (IBAction)WebsiteBack:(id)sender{
    if ([webView canGoBack]) {
        [webView goBack];
    }
}

//go forward in stack (built in)
- (IBAction)WebsiteForward:(id)sender{
    if ([webView canGoForward]) {
        [webView goForward];
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

//animation when loading web page
- (void)webViewDidStartLoad:(UIWebView *)webView{
    navigationLoading.hidden = false;
    navigationComplete.hidden = true;
    activityIndicator.hidden = false;
    [activityIndicator startAnimating];
}

//finish animations when web page is loaded
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    navigationLoading.hidden = true;
    navigationComplete.hidden = false;
    [activityIndicator stopAnimating];
    activityIndicator.hidden = true;
}

//lost connection of failed to load a page
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    navigationLoading.hidden = true;
    navigationComplete.hidden = false;    
    [activityIndicator stopAnimating];    
    activityIndicator.hidden=true;

}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [webView  loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.ca/m/places?hl=en#ipd:mode=search&q=pharmacy&source=mus"]]]; // go straight to google places
    
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc{
    
    [mm release];
    
    [webView release];
    [navigationComplete release];
    [navigationLoading release];
    [lastPageLoading release];
    [nextPageLoading release];
    [stopLoading release];
    [back release];
    [lastPage release];
    [nextPage release];
    [refresh release];
    [activityIndicator release];
    
    [super dealloc];
}

@end
